<?php

namespace app\management\controller;

use app\management\model\EveryTimes;
use app\management\model\User;
use app\Request;
use liliuwei\think\Jump;
use think\facade\View;
use app\management\model\Copy as copyModel;

class Copy
{

    use Jump;
    public function index(){
        return 'copy明细表';
    }

    ////////////////////////////////////
    /// 复制统计记录
    /// /////////////////////////////////
    /// 
     
    public function copyTimes(Request $request){
        $userData = get_admin_session();
        $user = new User();
        $res = $user->where('id',$userData['id'])->find();
        // var_dump($res->user_domain);exit;
        if (!empty($res->user_domain)) {
            $arr = array();
            foreach ($res->user_domain as $k=>$v){
                $arr[$k]['domain'] = $v;
            }
            View::assign('url',$arr);
        }else{
            View::assign('url','');
        }
        if ($request->param('domain')!=""){
            View::assign('urlDate',$request->param('domain'));
        }else{
            View::assign('urlDate','');
        }

       return View::fetch();

    }


    public function copyList(Request $request){
        $userData = get_admin_session();
        $user = new User();
        $res = $user->where('id',$userData['id'])->find();
        $copy = new copyModel();
        $input = $request->param();
       // var_dump($res);die;
        $url = array();
        if ($input['domain']==""){
            $url = $res->user_domain;

        }else{
            $url[] = $input['domain'];
        }

        $data = $copy->withSearch(['domain','create_time'],[
            'domain' =>$url,
            'create_time' =>$request->param('time')
        ])->where('user_id',$userData['id'])->order('id', 'desc')->page($request->param('page'),$request->param('limit'))->select()->toArray();
          
        $count =  $copy->withSearch(['domain','create_time'],[
            'domain' =>$url,
            'create_time' =>$request->param('time')
        ])->count();
        
        // 处理BUG:Malformed UTF-8 characters, possibly incorrectly encoded
        // 是返回的数据有特殊字符，编码转换回来就好了
        // 网址：https://www.cnblogs.com/yehuisir/p/11909427.html
        // mb_convert_encoding($value, 'UTF-8', 'UTF-8');
        // 中文乱码可以用以下方法转码
        // $content = mb_convert_encoding( $result, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' );
        // 意思就是把UTF-8,GBK,GB2312,BIG5这几种编码转成UTF-8编码。
        // 因为在$data数组中存在特殊字符，所以需要转换；$count不需要
        $data = mb_convert_encoding( $data, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' );
        // $count = mb_convert_encoding( $count, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' );
        return lay_msg(0,'success',$count,$data);
    }


    public function delete(Request $request){
        $copy = new copyModel();
        // var_dump($request->param());exit;
        if ($request->isPost()) {
             // 传入数组批量删除数据
            $copy->destroy($request->param('id'));
        }else{
             // 单条数据删除
            $copy->where('id',$request->param('id'))->delete();
        }
       return $this->success('删除成功');
    }



    ////////////////////////////////////
    /// 访客记录
    /// /////////////////////////////////
    ///

    public function customTimes(Request $request){
        $userData = get_admin_session();
        $user = new User();
        $res = $user->where('id',$userData['id'])->find();

        if (!empty($res->user_domain)) {
            $arr = array();
            foreach ($res->user_domain as $k=>$v){
                $arr[$k]['domain'] = $v;
            }
            View::assign('url',$arr);
        }else{
            View::assign('url','');
        }

        if ($request->param('domain')!=""){
            View::assign('urlDate',$request->param('domain'));
        }else{
            View::assign('urlDate','');
        }
        return View::fetch();
    }

    // 根据域名/时间段查询记录
    public function customList(Request $request){
        $userData = get_admin_session();
        $user = new User();
        $res = $user->where('id',$userData['id'])->find();
        $everyTimes = new EveryTimes();
        $input = $request->param();
        $url = array();
        if ($input['domain']==""){
            $url = $res->user_domain;
        }else{
            $url[] = $input['domain'];
        }

        $data = $everyTimes->withSearch(['domain','create_time'],[
            'domain' =>$url,
            'create_time' =>$request->param('time')
        ])->where('user_id',$userData['id'])->order('id', 'desc')->page($request->param('page'),$request->param('limit'))->select()->toArray();

        $count =  $everyTimes->withSearch(['domain','create_time'],[
            'domain' =>$url,
            'create_time' =>$request->param('time')
        ])->count();

        return lay_msg(0,'success',$count,$data);
    }


    // 访客信息记录删除
    // 传入单个id或id数组（配合批量删除）进行删除
    public function customDelete(Request $request){
        // var_dump($request->param());exit;
        $everyTimes= new EveryTimes();
        if ($request->isPost()) {
            // 传入数组批量删除数据
           $everyTimes->destroy($request->param('id'));
       }else{
            // 单条数据删除
            $everyTimes->where('id',$request->param('id'))->delete();
       }
        return $this->success('删除成功');

    }


}
